package net.kunyv.base.controller;

import net.kunyv.base.entity.SysResource;
import net.kunyv.base.entity.SysUser;
import net.kunyv.base.service.SysResourceService;
import net.kunyv.log.ActLog;
import net.kunyv.log.OperType;
import net.kunyv.util.HdUtil;
import net.kunyv.util.PageResult;
import net.kunyv.util.PageParam;
import net.kunyv.util.ResourceTree;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author sunyb
 * @create 2017-08-02 15:57
 */
@Controller
@RequestMapping(value = "/SysResource")
public class SysResourceController {
    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 跳转菜单管理界面
     *
     * @param
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2018/12/7
     */
    @RequiresPermissions("resource:query")
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    public String resource() {
        return "login/system/resource";
    }

    /**
     * main.jsp获取当前用户菜单树
     *
     * @return List<ResourceTree>
     * @throw
     * @author sunyb
     * @date 2017/8/2
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getUserMenu")
    public List<ResourceTree> getUserMenu() {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        return sysResourceService.getUserResourceTreeListByUserId(loginUser.getUserId());
    }

    /**
     * 菜单管理--保存菜单
     *
     * @param tree
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2017/1/9
     */
    @ResponseBody
    @RequiresPermissions(value = {"resource:edit", "resource:add"}, logical = Logical.OR)
    @ActLog(value = "保存菜单", type = OperType.AddOrUpdate)
    @RequestMapping("save")
    public String save(ResourceTree tree) {
        SysUser loginUser = HdUtil.getLoginUser();
        return sysResourceService.save(tree, loginUser);
    }

    /**
     * 资源管理--删除资源
     *
     * @param resourceId
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2018/12/7
     */
    @ResponseBody
    @RequiresPermissions("resource:delete")
    @ActLog(value = "删除资源", type = OperType.DELETE)
    @RequestMapping("delete")
    public String delete(String resourceId) {

        return sysResourceService.deleteResourceAndChinldren(resourceId);
    }

    /**
     * 资源管理--获取所有菜单
     *
     * @param request
     * @return
     * @author sunyb
     */
    @ResponseBody
    @RequiresPermissions("resource:query")
    @RequestMapping(method = RequestMethod.GET, value = "/getAllMenu")
    public List getAllMenu(HttpServletRequest request) {
        return sysResourceService.getAllMenu();
    }

    /**
     * 资源管理--点击菜单，获取权限列表
     *
     * @param resourceId 菜单ID
     * @param pageParam  分页参数
     * @return net.kunyv.base.entity.EasyuiDatagridResult
     * @throw
     * @author sunyb
     * @date 2017/1/9
     */
    @ResponseBody
    @RequiresPermissions("resource:query")
    @RequestMapping("getPermissionByResourceId")
    public PageResult getPermissionByResourceId(String resourceId, PageParam pageParam) {
        return sysResourceService.getPermissionByResourceId(resourceId, pageParam);
    }

    /**
     * 资源管理--保存权限
     *
     * @param resource
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2017/1/9
     */
    @ResponseBody
    @RequiresPermissions(value = {"resource:edit", "resource:add"}, logical = Logical.OR)
    @ActLog(value = "保存权限", type = OperType.AddOrUpdate)
    @RequestMapping("savePermission")
    public String savePermission(SysResource resource) {
        return sysResourceService.savePermission(resource);
    }
}
